package com.ruoyi.pyo.yunxin;


import com.netease.nim.server.sdk.core.Result;
import com.netease.nim.server.sdk.core.YunxinApiHttpClient;
import com.netease.nim.server.sdk.core.exception.YunxinSdkException;
import com.netease.nim.server.sdk.im.v2.YunxinV2ApiServices;
import com.netease.nim.server.sdk.im.v2.account.IAccountV2Service;
import com.netease.nim.server.sdk.im.v2.account.request.CreateAccountRequestV2;
import com.netease.nim.server.sdk.im.v2.account.response.CreateAccountResponseV2;

/**
 * Created by caojiajun on 2025/5/26
 */
public class YunxinApiHttpClientInit {

    private static YunxinApiHttpClient yunxinApiHttpClient;
    private static YunxinV2ApiServices services = new YunxinV2ApiServices(initWithEndPoint("https://open.yunxinapi.com"));
    private static String appkey = "4a0909af69667f8a601a747f7340474a";
    private static String appsecret = "cefb8eea4817";

    public static YunxinApiHttpClient initWithEndPoint(String endpoint) {
        if (yunxinApiHttpClient != null) {
            return yunxinApiHttpClient;
        }
        int timeoutMillis = 5000;
        yunxinApiHttpClient = new YunxinApiHttpClient.Builder("4a0909af69667f8a601a747f7340474a", "cefb8eea4817")
                .timeoutMillis(timeoutMillis)
                .endpoint(endpoint)
                .build();
        return yunxinApiHttpClient;
    }


    private static String createAccount(String accountId, String name) throws YunxinSdkException {
        CreateAccountRequestV2 request = new CreateAccountRequestV2();
        // 网易云信账号，请确保唯一性，只允许字母、数字、半角下划线、@、半角点以及半角横线，长度上限为 32 位字符
        request.setAccountId(accountId);
        // 登录密钥 Token，长度上限 128 位字符
        //request.setToken(UUID.randomUUID().toString().replaceAll("-", ""));

        // 设置用户信息
        CreateAccountRequestV2.UserInformation userInfo = new CreateAccountRequestV2.UserInformation();
        // 用户昵称，长度上限 64 位字符
        userInfo.setName(name);
        // 用户头像URL，长度上限 1024 位字符
        //userInfo.setAvatar("https://example.com/avatar.jpg");
        // 用户签名，长度上限 256 位字符
        //userInfo.setSign("This is a test signature");
        // 用户邮箱，长度上限 64 位字符
        //userInfo.setEmail("test@example.com");
        // 用户生日，格式如 "xxxx-xx-xx"
        //userInfo.setBirthday("1990-01-01");
        // 用户手机号，长度上限 32 位字符
        //userInfo.setMobile("13800138000");
        // 用户性别，0-未知，1-男，2-女
        //userInfo.setGender(1);
        // 扩展字段，建议JSON格式，长度上限 1024 位字符
        // userInfo.setExtension("{\"custom_key\":\"custom_value\"}");
        request.setUserInformation(userInfo);

        // 设置安全通相关配置
        /*CreateAccountRequestV2.AntispamConfiguration antispamConfig = new CreateAccountRequestV2.AntispamConfiguration();
        antispamConfig.setEnabled(true);
        // 设置安全通业务ID列表（嵌套在antispam_configuration内）
        java.util.List<CreateAccountRequestV2.BusinessIdMapItem> businessIdMap = new java.util.ArrayList<>();
        CreateAccountRequestV2.BusinessIdMapItem textItem = new CreateAccountRequestV2.BusinessIdMapItem();
        textItem.setType(1);
        textItem.setBusinessId("your_text_business_id");
        businessIdMap.add(textItem);
        antispamConfig.setBusinessIdMap(businessIdMap);
        request.setAntispamConfiguration(antispamConfig);*/

        /*// 设置配置项
        CreateAccountRequestV2.Configuration config = new CreateAccountRequestV2.Configuration();
        // 账号是否可用，默认为true
        config.setEnabled(true);
        // 单聊禁言标记，true表示禁言
        config.setP2pChatBanned(false);
        // 群聊禁言标记，true表示禁言
        config.setTeamChatBanned(false);
        // 聊天室禁言标记，true表示禁言
        config.setChatroomChatBanned(false);
        // 圈组禁言标记，true表示禁言
        config.setQchatChatBanned(false);
        request.setConfiguration(config);*/

        IAccountV2Service accountService = services.getAccountService();
        Result<CreateAccountResponseV2> result = accountService.createAccount(request);
        System.out.println("createAccount:" + result.getMsg());
        if (200 == result.getCode()) {
            return result.getResponse().getToken();
        }
        throw new RuntimeException(result.getMsg());
    }

    public static void main(String[] args) {
        System.out.println(createAccount("9531","9531"));
    }
}